/* Copyright (C) 2015 Daniyaal Rasheed <radonapps@gmail.com>

   This file is part of the Dekko email client for Ubuntu Touch/

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License or (at your option) version 3

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IMAPLOGMODEL_H
#define IMAPLOGMODEL_H

#include <QObject>
#include <QDateTime>
#include <QPointer>
#include <app/Settings/Settings.h>
#include "app/Accounts/Account.h"
#include "app/Components/QmlObjectListModel.h"

#define LOG_LIMIT 250

namespace Dekko {

/* LogItem is a small class to hold the logged data
 *
 * It is made available in qml via the ImapLog::log model
 */
class LogItem : public QObject
{
    Q_OBJECT
    // the text to display in the UI
    Q_PROPERTY(QString text READ text CONSTANT)
    // The time this was created. Could be handy when pruning the model for items older than X minutes/hours.
    Q_PROPERTY(QDateTime time READ time CONSTANT)
public:
    explicit LogItem(QObject *parent = 0);

    QString text() const;
    void setText(const QString &text);
    QDateTime time() const;
    void setTime(const QDateTime &time);

private:
    QString m_text;
    QDateTime m_time;
};

class ImapLog : public QObject
{
    Q_OBJECT
    // Account the imap model belongs to
    Q_PROPERTY(QObject *account READ account WRITE setAccount NOTIFY accountChanged)
    Q_PROPERTY(QObject *log READ log NOTIFY accountChanged)
    // Could be useful in the UI... just a quick way of getting account()->profile()->description()
    Q_PROPERTY(QString description READ description NOTIFY accountChanged)
public:
    explicit ImapLog(QObject *parent = 0);

    QObject *account() const;
    QObject *log() const;
    QString description() const;
    // Set the internal account and connect up the logged signal
    void setAccount(QObject *account);

signals:
    void accountChanged();

public slots:
    void handleSomethingLogged(uint parserId, const Common::LogMessage &message);

private:
    //enum to describe different log types
    enum Type {INVALID, IO_READ, IO_WRITE, OTHER};

    QString formatMessage(const QDateTime &timeStamp, const QString &message, const Type &type = INVALID);
    QPointer<Accounts::Account> m_account;
    QQmlObjectListModel<LogItem> *m_log;
};

}
#endif // IMAPLOGMODEL_H
